package features

import (
	"context"
	"fmt"
	"gitee.com/chunanyong/zorm"
)

func IsMysql(ctx context.Context, dbs *DBS) {

	//1.查询多少张表
	tables := make([]*TableStruct, 0)
	finder := zorm.NewSelectFinder(" INFORMATION_SCHEMA.TABLES ")
	finder.Append(" WHERE  TABLE_SCHEMA = ?", dbs.DBName)
	err := zorm.Query(ctx, finder, &tables, nil)
	if err != nil {
		fmt.Errorf("查询数据库异常", err)
		panic(err)
	}

	//2.查询各个表结构
	for _, t := range tables {
		finder2 := zorm.NewFinder()
		finder2.Append("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT  from INFORMATION_SCHEMA.COLUMNS  WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?  ORDER BY ORDINAL_POSITION  ASC ", dbs.DBName, t.TableName)
		maps, err := zorm.QueryMap(ctx, finder2, nil)
		if err != nil {
			fmt.Errorf("查询表字段异常：", err)
		}

		//3.查找各个表主键
		finderPK := zorm.NewFinder()
		finderPK.Append("SELECT column_name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA=? and  table_name=? AND constraint_name=?", dbs.DBName, t.TableName, "PRIMARY")
		pkName := ""
		zorm.QueryRow(ctx, finderPK, &pkName)

		//处理数据
		info := typeConversion(maps, pkName, t, dbs)

		//7.在这里生成具体文件
		GenerateFile(info, dbs)
	}

}
